<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;


class LoginController extends Controller
{
    //login
    public function login()
    {
    	return view('admin.login.login');
    }
    
    //doLogin
    public function doLogin(Request $request)
    {
    	$data = $request -> except('_token');
    	//验证人验证码
    	if($data['code'] == session('milkcaptcha'))
    	{
    		//根据邮箱查询信息
    		$res = DB::table('users') -> where('email', $data['email']) -> first();
    		//验证邮箱并且必须是管理员
    		if($res && $res -> auth == 1 && $res -> status == 0)
    		{
    			//解密密码
	    		$password = Crypt::decrypt($res -> password);
	    		//验证密码
	    		if($data['password'] == $password)
	    		{
                    $userd = DB::table('userdetail') -> where('id', $res -> id) -> first();
	    			session(['masters' => $res]);
                    session(['users' => $userd]);
	    			return redirect('/admin/index/index');
	    		}else
	    		{
	    			return back() -> with(['info' => '密码错误']);
	    		}
	    	}else
	    	{
	    		return back() -> with(['info' => '邮箱错误']);
	    	}

    	}else
    	{
    		return back() -> with(['info' => '验证码错误']);
    	}
    }

    //注销
    public function cancel()
    {
    	//销毁session登录信息
    	session()->flush('master');
    	return redirect('/admin/login');
    }

}
